package com.jy.appmarket.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.jy.appmarket.R;

public class FragmentRank extends Fragment implements View.OnClickListener {
	// private static final String TAG = "FragmentRank";
	// private Activity mActivity;
	private FragmentTops mDownloadFragment;
	private FragmentTops mRiseFragment;
	private FragmentTops mPraiseFragment;
	private int mCurrentIndex = 0;
	private boolean mIsCreated = false;
	private RadioButton mDownload, mRise, mPraise;
	private static final String TAG_DOWNLOAD = "download";
	private static final String TAG_RISE = "rise";
	private static final String TAG_PRAISE = "praise";

	@Override
	public void onAttach(Activity activity) {
		// mActivity = activity;
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mIsCreated = true;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.rank_fragment, container, false);
		mDownload = v.findViewById(R.id.rank_fragment_download);
		mDownload.setOnClickListener(this);

		mRise = v.findViewById(R.id.rank_fragment_rise);
		mRise.setOnClickListener(this);

		mPraise = v.findViewById(R.id.rank_fragment_praise);
		mPraise.setOnClickListener(this);

		mDownloadFragment = new FragmentTops();
		mRiseFragment = new FragmentTops();
		mPraiseFragment = new FragmentTops();
		Fragment f1 = getFragmentManager().findFragmentByTag(TAG_DOWNLOAD);
		Fragment f2 = getFragmentManager().findFragmentByTag(TAG_RISE);
		Fragment f3 = getFragmentManager().findFragmentByTag(TAG_PRAISE);

		FragmentTransaction ft = getFragmentManager().beginTransaction();
		try {
			if (f1 != null) {
				ft.remove(f1);
			}
			if (f2 != null) {
				ft.remove(f2);
			}
			if (f3 != null) {
				ft.remove(f3);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		ft.add(R.id.rank_fragment_content, mDownloadFragment, TAG_DOWNLOAD);
		ft.add(R.id.rank_fragment_content, mRiseFragment, TAG_RISE);
		ft.add(R.id.rank_fragment_content, mPraiseFragment, TAG_PRAISE);
		ft.hide(mDownloadFragment);
		ft.hide(mRiseFragment);
		ft.hide(mPraiseFragment);
		ft.commit();
		return v;
	}

	private void setCurrentFragment(int i) {
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		if (i == 0) {
			if (!mDownloadFragment.isVisible()) {
				mDownloadFragment.setUserVisibleHint(true);
				ft.show(mDownloadFragment);
			}
			if (mRiseFragment.isVisible()) {
				mRiseFragment.setUserVisibleHint(false);
				ft.hide(mRiseFragment);
			}
			if (mPraiseFragment.isVisible()) {
				mPraiseFragment.setUserVisibleHint(false);
				ft.hide(mPraiseFragment);
			}
		} else if (i == 1) {
			if (mDownloadFragment.isVisible()) {
				mDownloadFragment.setUserVisibleHint(false);
				ft.hide(mDownloadFragment);
			}
			if (!mRiseFragment.isVisible()) {
				mRiseFragment.setUserVisibleHint(true);
				ft.show(mRiseFragment);
			}
			if (mPraiseFragment.isVisible()) {
				mPraiseFragment.setUserVisibleHint(false);
				ft.hide(mPraiseFragment);
			}
		} else if (i == 2) {
			if (mDownloadFragment.isVisible()) {
				mDownloadFragment.setUserVisibleHint(false);
				ft.hide(mDownloadFragment);
			}
			if (mRiseFragment.isVisible()) {
				mRiseFragment.setUserVisibleHint(false);
				ft.hide(mRiseFragment);
			}
			if (!mPraiseFragment.isVisible()) {
				mPraiseFragment.setUserVisibleHint(true);
				ft.show(mPraiseFragment);
			}
		}
		ft.commit();
		setTabs(i);
	}

	private void setTabs(int index) {
		switch (index) {
		case 0:
			mDownload.setChecked(true);
			break;
		case 1:
			mRise.setChecked(true);
			break;
		case 2:
			mPraise.setChecked(true);
			break;
		default:
			mDownload.setChecked(true);
		}

	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (!mIsCreated) {
			return;
		}
		if (isVisibleToUser) {
			setCurrentFragment(mCurrentIndex);
		} else {
			// onPause
		}
	}

	@Override
	public void onClick(View v) {
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		switch (v.getId()) {
		case R.id.rank_fragment_download:
			setCurrentFragment(0);
			mCurrentIndex = 0;
			break;
		case R.id.rank_fragment_rise:
			setCurrentFragment(1);
			mCurrentIndex = 1;
			break;
		case R.id.rank_fragment_praise:
			setCurrentFragment(2);
			mCurrentIndex = 2;
			break;
		}
		ft.commit();
	}
}